<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of private_messages_model
 *
 * @author win7
 */
class private_messages_model extends admin_model {

    //put your code here
    public function __construct() {
        parent::__construct();
    }

    /**
     * 
     * @return type
     */
    public function filter($nolimit = false) {
        /* ======== filter ======== */




        // sender
        if (metohd_get_uri('sender')) {
            $userid = get_userid_with_username(metohd_get_uri('sender'));
            $this->db->where_in('private_messages.sender_id', $userid);
        }

        // reciver
        if (metohd_get_uri('reciver')) {
            $userid = get_userid_with_username(metohd_get_uri('reciver'));
            $this->db->where_in('private_messages.reciver_id', $userid);
        }

        // subject
        if (metohd_get_uri('subject')) {
            $this->db->like('private_messages.subject', metohd_get_uri('subject'));
        }
        // message
        if (metohd_get_uri('message')) {
            $this->db->like('private_messages.message', metohd_get_uri('message'));
        }

        // statues
        if (metohd_get_uri('statues')) {
            $ststues=array('1'=>'0','2'=>'1','3'=>'2');
            $this->db->where('private_messages.statues', $ststues[metohd_get_uri('statues')]);
        }

        // Create_date
        if (metohd_get_uri('create_date')) {
            $date = date('Y-m-d H:i:s', strtotime(metohd_get_uri('create_date')));
            $this->db->where('private_messages.create_date >=', $date);
        }




        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);


        $statrt_limit = get_segment(4, 0);
        if ($nolimit == false) {
            $this->db->limit($limit, $statrt_limit);
        }


        //$this->db->select('users.username ,private_messages.* ', false);
        $this->db->order_by('private_messages.id ' . metohd_get_uri('sort', 'desc'));
       // $this->db->or_where('private_messages.sender_id  IS NULL');
       // $this->db->join('users ', 'users.id=private_messages.sender_id');
        $this->db->from('private_messages');


        $sql = $this->db->get();

        return $sql->result();
       
    }

    function add_reply($data,$email='') {
        if(empty($email)){
         $site_info=  site_option();
         $this->email_system->SendEmail(array('email'=>$email),$site_info,'تلقيت رد علي استفسارك ', $data['comment']);   
        }
        $this->db->insert('private_message_replies', $data);
        $this->incrment('private_messages', 'count_replay', 1, array('id' => $data['message_id']));
        $this->db->update('private_messages', array('statues' => '2'), array('id' => $data['message_id']));  
        
        $this->add_manager_report(array('action' => 'private_message_add-replay', 'contant' => $data));

    }

    function delete_reply($id, $message_id) {
        $this->db->delete('private_message_replies', array('id' => $id));
        $this->incrment('private_messages', 'count_replay', 1, array('id' => $message_id), '-');
        $this->add_manager_report(array('action' => 'private_message_delete-replay', 'contant' => $id));
    }

    /**
     * 
     * @param type $id
     */
    function delete_message($id) {

        $this->db->where_in('id', $id);
        $this->db->delete('private_messages', array('id' => $id));
        $this->db->where_in('message_id', $id);
        $this->db->delete('private_message_replies', array('message_id' => $id));
        $this->add_manager_report(array('action' => 'private_message-delete', 'contant' => $id));
    }
    
    /**
     * 
     * @param type $data
     */
    function insert_message($data){
        $this->db->insert('private_messages',$data);
        
        $data['id']=$this->db->insert_id();
        $this->add_manager_report(array('action' => 'private_message-add', 'contant' => $data));
    }

    function update_message($data,$id){
        $this->db->update('private_messages',$data,array('id'=>$id));
        $data['id']=$id;
        $this->add_manager_report(array('action' => 'private_message-edit', 'contant' => $data));
    }

    
    public function update_all($id, $data, $where = '') {
        if (count($id) > 0) {
            $this->db->where_in('id', $id);
        }
        $data['user_update'] = $this->admin_id;
        $this->db->update('private_messages', $data, $where);
        
        $data['id']=  join(',', $id);
        $this->add_manager_report(array('action' => 'private_message-edit', 'contant' => $data));
    }    
    
}

?>
